package game.pong.business
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Gamespeed extends Timer
	{
		private var initialDuration : Number = 30;
		private var timerDuration : Number;
		private var speedUp : Boolean;
		
		private var internalTimer : Timer = new Timer(5000);
		
		public function Gamespeed(
									initialDuration : Number = 30, 
									changeSpeedDelay : Number = 5000,
									speedUp : Boolean = true
									)
		{
			this.initialDuration = initialDuration;
			this.speedUp = speedUp;
			timerDuration = initialDuration;
			
			super(initialDuration, 0);
			
			internalTimer = new Timer(changeSpeedDelay, 0);
			internalTimer.addEventListener(TimerEvent.TIMER, changeSpeed);
		}
		
		protected function changeSpeed(event : TimerEvent) : void
		{
			if (timerDuration > 0)
			{
				if (speedUp)
					--timerDuration;
				else
					++timerDuration;					
				delay = timerDuration;
				reset();
				start();
			}
		}
		
		override public function start() : void
		{
			super.start();
			internalTimer.start();
		}
		
		
		override public function stop() : void
		{
			super.stop();
			internalTimer.stop();
		}
		
		public function resetSpeed() : void
		{
			delay = initialDuration;
			reset();
		}
		
	}
}